﻿Type.registerNamespace('MS.InformationManagement.DocumentGenerator');
MS.InformationManagement.DocumentGenerator.NewDocumentTimeout = null;
MS.InformationManagement.DocumentGenerator.Notification = null;
MS.InformationManagement.DocumentGenerator.IsValid = null;

MS.InformationManagement.DocumentGenerator.InitializeCreationForm = function (documentNameBox, libraryIdHidden, templatePathHidden, submitButton, preloader) {
    $('#document-generator').ajaxloader({ fullscreen: true, baseurl: '/sites/docs/', autoshow: false, usealternatesettings: true });
    MS.InformationManagement.Services.DocumentGeneratorService.set_path('/sites/docs' + MS.InformationManagement.Services.DocumentGeneratorService.get_path());
    var nameTextBox = $('#' + documentNameBox);
    var libraryIdHiddenField = $('#' + libraryIdHidden);
    var templatePathHiddenField = $('#' + templatePathHidden);
    var spinner = $('#' + preloader);
    var button = $('#' + submitButton);

    button.click(function () {
        var validForm = Page_ClientValidate();
        if(validForm === true){
            MS.InformationManagement.DocumentGenerator.IsValid = 'No valido';
            MS.InformationManagement.DocumentGenerator.GenerateDocument(nameTextBox, libraryIdHiddenField, templatePathHiddenField, button);
        }
    });

    nameTextBox.bind("propertychange keyup input paste",
        function (event) {
            clearTimeout(MS.InformationManagement.DocumentGenerator.NewDocumentTimeout);
            MS.InformationManagement.DocumentGenerator.NewDocumentTimeout = setTimeout(
                function () {
                    var validForm = Page_ClientValidate();
                    if(validForm === true){
                        MS.InformationManagement.DocumentGenerator.DocumentExistsOnCreate(nameTextBox, libraryIdHiddenField, templatePathHiddenField, button, spinner);
                    }
                }, 1000);
        }
    );
}

MS.InformationManagement.DocumentGenerator.DocumentExistsOnCreate = function (nameTextBox, libraryIdHiddenField, templatePathHiddenField, button, spinner) {
    if (templatePathHiddenField.val() == '') {
        MS.InformationManagement.Common.ShowStatus('Debe seleccionar una plantilla antes de ingresar el nombre del documento.', 'orange', 6000);
        return;
    }
    if ($.trim( nameTextBox.val()) != '' && MS.InformationManagement.DocumentGenerator.IsValid == null) {
        spinner.addClass('inline');
        MS.InformationManagement.Services.DocumentGeneratorService.DocumentExists(
            nameTextBox.val(),
            libraryIdHiddenField.val(),
            templatePathHiddenField.val(),
            MS.InformationManagement.DocumentGenerator.OnAjaxComplete,
            MS.InformationManagement.DocumentGenerator.OnAjaxException,
            { submitButton: button, preloader: spinner }
        );
    }
}

MS.InformationManagement.DocumentGenerator.GenerateDocument = function (nameTextBox, libraryIdHiddenField, templatePathHiddenField, button) {
    if ($.trim( nameTextBox.val()) != '') {
        $('#document-generator').ajaxloader('show');
        MS.InformationManagement.Services.DocumentGeneratorService.GenerateDocument(
            nameTextBox.val(),
            libraryIdHiddenField.val(),
            templatePathHiddenField.val(),
            MS.InformationManagement.DocumentGenerator.OnDocumentGenerationComplete,
            MS.InformationManagement.DocumentGenerator.OnAjaxException,
            { submitButton: button }
        );
    } else { MS.InformationManagement.Common.ShowStatus('El nombre del documento es de carácter obligado.', 'orange', 6000); }
}

MS.InformationManagement.DocumentGenerator.OnDocumentGenerationComplete = function (result, userState) {
    $('#document-generator').ajaxloader('hide');
    editDocumentWithProgID2(result, '', 'SharePoint.OpenDocuments', '0', '', '0');
    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK);
}


MS.InformationManagement.DocumentGenerator.OnAjaxComplete = function (result, userState) {
    userState.preloader.removeClass('inline');
    if (MS.InformationManagement.DocumentGenerator.Notification != null) { MS.InformationManagement.DocumentGenerator.Notification.remove(); }
    if (result == true) {
        userState.submitButton.attr('disabled', 'disable');
        MS.InformationManagement.DocumentGenerator.Notification = $('<span class="notification warning">Ya existe un documento con este nombre.</span>');
    }
    else {
        userState.submitButton.removeAttr('disabled');
        MS.InformationManagement.DocumentGenerator.Notification = $('<span class="notification ok">El nombre se encuentra disponible.</span>');
    }
    MS.InformationManagement.DocumentGenerator.Notification.insertAfter(userState.preloader);
}

MS.InformationManagement.DocumentGenerator.OnAjaxException = function (exception, userState) {
    if (typeof userState.preloader != 'undefined' && userState.preloader != '') { userState.preloader.removeClass('inline'); }
    try { $('#document-generator').ajaxloader('hide'); } catch (e) { }
    userState.submitButton.attr('disabled', 'disable');
    MS.InformationManagement.Common.ShowStatus('Ha ocurrido una excepción mientras se procesaba su peticion: ' + exception.get_message(), 'red', 6000);
}